﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace _201206QuanLyBanHang.BaoCao
{
    public partial class FormDSKhachHang : Form
    {
        private ThuVien.Class_Database db = new ThuVien.Class_Database();
        BLL_BaoCao bc = new BLL_BaoCao();
        DataSet ds = new DataSet();
        public FormDSKhachHang()
        {
            InitializeComponent();
            loadComBo();
        }

        //Load dữ liệu vào combo
        void loadComBo()
        {
            db.fillCombo("select manhanvien, honhanvien + ' ' + tennhanvien as hoten from nhanvien where tinhtrangnv=1", comboBox_NhanVienGiao, "hoten", "manhanvien");
            db.fillCombo("select manhanvien, honhanvien + ' ' + tennhanvien as hoten from nhanvien where tinhtrangnv=1", comboBox_NhanVienNhan, "hoten", "manhanvien");
            db.fillCombo("select makhuvuc,tenkhuvuc from khuvuc", comboBox_KhuVuc, "tenkhuvuc", "makhuvuc");
            comboBox_NhanVienGiao.SelectedIndex = -1;
            comboBox_NhanVienNhan.SelectedIndex = -1;
            comboBox_KhuVuc.SelectedIndex = -1;
            comboBox_KhuVuc.Text = "--Chọn khu vực giao--";
            comboBox_NhanVienNhan.Text = "--Nhân viên nhận sản phẩm--";
            comboBox_NhanVienGiao.Text = "--Nhân viên giao sản phẩm--";

            //ds=db.ds_dataset("select tensanpham from sanpham");
            dataGridView_SanPhamSL.DataSource = db.getDatatable("select tensp from sanpham");
        }
        //Load dữ liệu vào reportViewer
        private void loaddulieuvaoreport(string strbien)
        {
            DataTable table = new DataTable();
            DataTable tb2 = new DataTable();
            table.Clear();
            tb2.Clear();
            //table = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            table = desDSKhachHangKV("");
            tb2 = db.getDatatable("select machiphi,tenchiphi from chiphididuong");
            //reset lai khung hiển thị report
            reportViewer_DSKhachHang.Reset();
            //gán tên report cần hiển thị trong khung nhìn viewer
            //reportViewer1.LocalReport.ReportPath = Application.StartupPath + "\\report1.rdlc";
            //reportViewer_DSKhachHang.LocalReport.ReportPath = Application.StartupPath + "\\BaoCao\\rptDSKhachHangKV.rdlc";
            reportViewer_DSKhachHang.LocalReport.ReportPath = @"D:\Project\Soft\code 03-10-2012 - tháng 10\201206QuanLyBanHang\BaoCao\rptDSKhachHangKV.rdlc";// @"\report\rpt_sanpham.rdlc";
            //rptViewer.LocalReport.ReportPath = @"D:\Project\Soft\code 03-10-2012 - tháng 10\201206QuanLyBanHang\BaoCao\rptDanhSachKhachHang.rdlc";// @"\report\rpt_sanpham.rdlc";
            // rptViewer.LocalReport.ReportEmbeddedResource = "201206QuanLyBanHang.BaoCao.CongNo_KhachHang.rdlc";
            
            reportViewer_DSKhachHang.LocalReport.SetParameters(new ReportParameter( "ReportParameterKhuVuc", comboBox_KhuVuc.Text));
            reportViewer_DSKhachHang.LocalReport.SetParameters(new ReportParameter("ReportParameterNVGiao", comboBox_NhanVienGiao.Text));
            reportViewer_DSKhachHang.LocalReport.SetParameters(new ReportParameter("ReportParameterNVNhan", comboBox_NhanVienNhan.Text));
            reportViewer_DSKhachHang.LocalReport.SetParameters(new ReportParameter("ReportParameterNgayGiao", dateTimePicker_NgayGiao.Value.ToShortDateString()));
            reportViewer_DSKhachHang.LocalReport.SetParameters( new ReportParameter("ReportParameter1","1"));
            //reportViewer_DSKhachHang.LocalReport.SetParameters(new ReportParameter("ReportParameter2", "Cơm"));
             
            reportViewer_DSKhachHang.LocalReport.DataSources.Clear();
            ReportDataSource newDataSource = new ReportDataSource("DataSetDSKhachHangKV", table);//("ds_sanpham", table);
            //ReportDataSource rpt2 = new ReportDataSource("DataSetDSChiPhiDiDuong", tb2);//("ds_sanpham", table);

            //reportViewer_DSKhachHang.LocalReport.DataSources.
            reportViewer_DSKhachHang.LocalReport.DataSources.Add(newDataSource);
            //lam tuoi report
            // rptViewer.LocalReport.DataSources.Add(new ReportDataSource("ds_sanpham",table);
            reportViewer_DSKhachHang.RefreshReport();
        }       
        private void FormDSKhachHang_Load(object sender, EventArgs e)
        {

            this.reportViewer_DSKhachHang.RefreshReport();
        }

        private void button_Xem_Click(object sender, EventArgs e)
        {
            henxui("");
            loaddulieuvaoreport("");

        }

        //Code thay đổi giá trị công nợ
        void henxui(string khuvuc)
        {
            
            //DataTable dt = new DataTable();
            //DataGridView dgv = new DataGridView();

            dataGridView_temp.DataSource = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            //dt = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            //int don = dt.Rows.Count;
            for (int i = 1; i < dataGridView_temp.RowCount; i++)
            {
                DataGridViewCell cellMaKH = dataGridView_temp.Rows[i - 1].Cells["tencuahang"];
                          String strMaKH = cellMaKH.FormattedValue.ToString();
                          DataGridViewCell cellMaKHtt = dataGridView_temp.Rows[i].Cells["tencuahang"];
                           String strMaKHtt = cellMaKHtt.FormattedValue.ToString();
                if (strMaKH == strMaKHtt)
                    dataGridView_temp.Rows[i].Cells["tongcongno"].Value = 0;
            }
          


        }
        public DataTable desDSKhachHangKV(string tenkhuvuc)
        {

            DataTable dt = new DataTable();
            //DataGridView dgv = new DataGridView();

            dataGridView_temp.DataSource = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            //dt = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
            //int don = dt.Rows.Count;
            for (int i = 1; i < dataGridView_temp.RowCount; i++)
            {
                DataGridViewCell cellMaKH = dataGridView_temp.Rows[i - 1].Cells["tencuahang"];
                String strMaKH = cellMaKH.FormattedValue.ToString();
                DataGridViewCell cellMaKHtt = dataGridView_temp.Rows[i].Cells["tencuahang"];
                String strMaKHtt = cellMaKHtt.FormattedValue.ToString();
                if (strMaKH == strMaKHtt)
                    dataGridView_temp.Rows[i].Cells["tongcongno"].Value = 0;
            }
            dt = dataGridView_temp.DataSource as DataTable;
            return dt ;

        }

        private void reportViewer_DSKhachHang_Load(object sender, EventArgs e)
        {

        }
        //void henxui(string khuvuc)
        //{
        //    dataGridView_temp.DataSource = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
        //    DataTable dt = new DataTable();
        //    dt = bc.getDSKhachHangKV(comboBox_KhuVuc.Text);
        //    //int don = dt.Rows.Count;
        //    for (int i = 1; i < dt.Rows.Count; i++)
        //    {
        //        DataGridViewCell cellMaKH = dataGridView_temp.Rows[i - 1].Cells["tencuahang"];
        //        String strMaKH = cellMaKH.FormattedValue.ToString();
        //        DataGridViewCell cellMaKHtt = dataGridView_temp.Rows[i].Cells["tencuahang"];
        //        String strMaKHtt = cellMaKHtt.FormattedValue.ToString();
        //        if (strMaKH == strMaKHtt)
        //            dataGridView_temp.Rows[i].Cells["tongcongno"].Value = 0;

        //    }

        //}
    }
}
